package com.simba.server.task;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import com.simba.server.components.CommandExecutor;
import com.simba.server.components.SortCommonDefines;
import org.apache.log4j.Logger;


import com.simba.common.log.LogUtil;

import com.simba.server.commands.impl.web.WebPacketScanCmd;
import com.simba.server.components.data.SortInfo;

/**
 * Created by kavin on 16-3-30.
 */
public class RecordScanMessageTask extends Thread
{
	public static final Logger logger = Logger.getLogger(RecordScanMessageTask.class);
	private final BlockingQueue<SortInfo> queue = new LinkedBlockingQueue<>();

	public RecordScanMessageTask()
	{
		this.setName("scan-message-Task");
	}

	public void offerScanMessageToQueue(SortInfo info)
	{
		if (info == null)
		{
			return;
		}
		this.queue.offer(info);
	}

	@Override
	public void run()
	{
		while (!Thread.currentThread().isInterrupted())
		{
			try
			{
				this.updateScanMessageList();
			}
			catch (Exception e)
			{
				if (e instanceof InterruptedException)
				{
					Thread.currentThread().interrupt();
				}
				LogUtil.LogError(RecordScanMessageTask.logger, "Handle scan message error:  " + e);
			}
		}
	}

	protected void updateScanMessageList() throws InterruptedException
	{
		SortInfo info = this.queue.take();

		if (info.getUploadFlag()== SortCommonDefines.UPLOAD_NO_UPLOAD_FLAG)
		{
			return;
		}
		if (info.getUploadFlag()==SortCommonDefines.UPLOAD_ONLY_PACK_FLAG)
		{
			return;
		}
		CommandExecutor.executorWebCommand(WebPacketScanCmd.NAME, info,false);

	}
}
